package com.graduation.roombooking.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.List;

/**
 * @Author:ZhaoXinYi
 * @FileName:
 * @Date:Created in  2022/4/2 23:47
 * @Version:
 * @Description:MVC配置类
 */

@Configuration
@EnableWebMvc
public class WebConfig implements WebMvcConfigurer {
//    @Autowired
//    private SampleInterceptor sampleInterceptor1;
//    @Autowired
//    private SampleInterceptor sampleInterceptor2;
//    @Autowired
//    private SampleInterceptor sampleInterceptor;


    @Autowired
    private UserArgumentResolver userArgumentResolver;

    @Override
    public void addArgumentResolvers(List<HandlerMethodArgumentResolver> resolvers) {
        resolvers.add(userArgumentResolver);
    }

    /**
     * 配置静态资源路径
     * @param registry
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {

        registry.addResourceHandler("/**")
                .addResourceLocations("classpath:/static/");
        registry.addResourceHandler("swagger-ui.html")
                .addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/");

    }
//    @Bean
//    public SampleInterceptor interceptor(){
//        return new SampleInterceptor();
//    }
//
//    /**
//     * 配置路径拦截器
//     * @param registry
//     */
//    @Override
//    public void addInterceptors(InterceptorRegistry registry) {
//        registry.addInterceptor(interceptor())
//                .addPathPatterns("/**")
//                .excludePathPatterns("/register/**","/login/**","/swagger-ui.html","/webjars/**","/**/swagger-resources/**","/error");
////        WebMvcConfigurer.super.addInterceptors(registry);
//    }

    /**
     * 配置全局跨域
     * @param registry
     */
    @Override
    public void addCorsMappings(CorsRegistry registry) {

        //添加映射路径
        registry.addMapping("/**")
                //是否发送Cookie
                .allowCredentials(true)
                //设置放行哪些原始域   SpringBoot2.4.4下低版本使用.allowedOrigins("*")
                .allowedOriginPatterns("*")
                //放行哪些请求方式
                .allowedMethods(new String[]{"GET", "POST", "PUT", "DELETE","OPTIONS"})
                //.allowedMethods("*") //或者放行全部
                //放行哪些原始请求头部信息
                .allowedHeaders("*")
                //暴露哪些原始请求头部信息
                .exposedHeaders("*");
    }

}